﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class DepoYerlesim : XForm
    {
        public DepoYerlesim()
        {
            InitializeComponent();
        }

        int AisleW = 50,    //30
            RackW = 30,     //13,
            RackH = 30,
            Offset = 20,
            MaxAdet = 50,
            MaxMetre = 0,
            TotalMetre = 0;

        double ScaleH = 1,
            ScaleV = 1;

        Dictionary<int, int> RackMetre = new Dictionary<int, int>();
        Dictionary<int, int> RackMetreORG = new Dictionary<int, int>();
        //Dictionary<int, decimal> RackMetre = new Dictionary<int, decimal>();

        #region Pens
        //Pen penCrimson1 = new Pen(Color.Crimson, 1);
        //Pen penDarkRed2 = new Pen(Color.DarkRed, 2);
        SolidBrush semiRed = new SolidBrush(Color.FromArgb(70, 255, 0, 0));
        SolidBrush semiRed2 = new SolidBrush(Color.FromArgb(200, 255, 0, 0));
        //SolidBrush semiSkyBlue = new SolidBrush(Color.FromArgb(140, Color.DeepSkyBlue.R, Color.DeepSkyBlue.G, Color.DeepSkyBlue.B));
        #endregion

        BindingSource bsYerlesim, bsYerlesimLines, bsYerlesimLinesAll;
        private void DepoYerlesim_Load(object sender, EventArgs e)
        {
            
            /*
            //DEFAULT NAMESPACE: "http://tempuri.org/MyData.xsd"
            DataTable dtYerlesim = new DataTable("Yerlesim");
            dtYerlesim.Columns.AddRange(new DataColumn[] {
                new DataColumn("CustomerId", typeof(int)),
                new DataColumn("TotalMts", typeof(int)),
                new DataColumn("TotalPcs", typeof(int))
            });

            dtYerlesim.PrimaryKey = new DataColumn[] { dtYerlesim.Columns["CustomerId"] };

            DataTable dtYerlesimLines = new DataTable("YerlesimLines");
            dtYerlesimLines.Columns.AddRange(new DataColumn[] {
                new DataColumn("CustomerId", typeof(int)),
                new DataColumn("RackNr", typeof(int)),
                new DataColumn("Mts", typeof(int))
            });

            //dtYerlesimLines.Constraints.Add(new ForeignKeyConstraint
            //   ("FK_YerlesimLines_Yerlesim_CustomerId", dtYerlesim.Columns["CustomerId"], dtYerlesimLines.Columns["CustomerId"]));

            DataTable dtYerlesimLinesAll = new DataTable("YerlesimLinesAll");
            dtYerlesimLinesAll.Columns.AddRange(new DataColumn[] {
                new DataColumn("RackNr", typeof(int)),
                new DataColumn("Mts", typeof(int))
            });

            dtYerlesimLinesAll.PrimaryKey = new DataColumn[] { dtYerlesim.Columns["RackNr"] };
            
            ds.Tables.Add(dtYerlesim);
            ds.Tables.Add(dtYerlesimLines);
            ds.Tables.Add(dtYerlesimLinesAll);

            ds.Tables["YerlesimLines"].Constraints.Add(new ForeignKeyConstraint
               ("FK_YerlesimLines_Yerlesim_CustomerId", ds.Tables["Yerlesim"].Columns["CustomerId"], ds.Tables["YerlesimLines"].Columns["CustomerId"]));
             * 
             * 
                ForeignKeyConstraint ParentFK = new ForeignKeyConstraint(rel.ParentColumns, rel.ChildColumns);
                ParentFK.UpdateRule = Rule.Cascade;
                rel.ChildTable.Constraints.Add(ParentFK);
             * 
           */


            panelControl3.Paint += new PaintEventHandler(GdiPlus_paint);    //eski hali this.Paint idi!
            panelControl3.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

            for (int i = 1; i < 10; i++)
                for (int j = 1; j < 12; j++)
                    RackMetreORG.Add(i * 100 + j, 0);

            for (int j = 1; j < 35; j++)
                RackMetreORG.Add(10 * 100 + j, 0);

            for (int k = 1; k < 50; k++)
                RackMetreORG.Add(11 * 100 + k, 0);

            for (int k = 1; k < 34; k++)
                RackMetreORG.Add(12 * 100 + k, 0);

            bsYerlesim = new BindingSource(ds, "xYerlesim");
            bsYerlesimLines = new BindingSource(bsYerlesim, "FK_xYerlesimLines_xYerlesim_CustomerId");
            bsYerlesimLinesAll = new BindingSource(ds, "xYerlesimLinesAll");

            //bsYerlesim = new BindingSource(ds, "Yerlesim");
            //bsYerlesimLines = new BindingSource(bsYerlesim, "FK_YerlesimLines_Yerlesim_CustomerId");
            //bsYerlesimLinesAll = new BindingSource(ds, "YerlesimLinesAll");

            InitGridView(gv, bsYerlesim);
            gv.AddSummary("TotalMts", "TotalPcs");
            gv.FormatAsNumber(0, "TotalMts", "TotalPcs");
            gv.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gv_FocusedRowChanged);

            GetDetails("xYerlesim;xYerlesimLines;xYerlesimLinesAll", "GetYerlesim");
            gv.BestFitColumns();

            foreach (MyData.xYerlesimLinesAllRow r in ds.xYerlesimLinesAll)
            {
                RackMetreORG[r.RackNr] = r.Mts;
                TotalMetre += r.Mts;
                if (r.Mts > MaxMetre)
                    MaxMetre = r.Mts;
            }
            MaxMetre = 25000;
            Notify("Tam Dolu Kare: " + MaxMetre.ToString() + " mt.");

            //Invalidate();
            FillRacks(intParse(bsYerlesim, "CustomerId"));
        }

        void gv_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            FillRacks(intParse(bsYerlesim, "CustomerId"));
        }

        void FillRacks(int CustomerId)
        {
            if (CustomerId > 0)
            {
                //foreach (KeyValuePair<int, int> p in RackMetre)
                //    RackMetre[p.Key] = 0;
                
                RackMetre.Clear();
                for (int i = 1; i < 10; i++)
                    for (int j = 1; j < 12; j++)
                        RackMetre.Add(i * 100 + j, 0);

                for (int j = 1; j < 35; j++)
                    RackMetre.Add(10 * 100 + j, 0);

                for (int k = 1; k < 5; k++)
                    RackMetre.Add(11 * 100 + k, 0);

                for (int k = 1; k < 34; k++)
                    RackMetre.Add(12 * 100 + k, 0);

                foreach (MyData.xYerlesimLinesRow r in ds.xYerlesimLines.Select("CustomerId = " + CustomerId.ToString()))
                    if (RackMetre.ContainsKey(r.RackNr))
                        RackMetre[r.RackNr] += r.Mts;

                panelControl3.Invalidate();
                //panelControl3.Update();
            }
        }


        #region Drawing
        void GdiPlus_paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.CompositingQuality = CompositingQuality.HighQuality;

            //if (e.ClipRectangle.Width * e.ClipRectangle.Height > 4 * RackW * RackH)
            //    ClearRackMetre();    //entire surface update edilecek


            foreach (KeyValuePair<int, int> p in RackMetreORG)
            {
                Rectangle rct = GetLocation(p.Key, RackMetreORG[p.Key], 1);     //TODO: raise'da hesaplanan rectangle nerede kullanılıyor?
                g.FillRectangle(semiRed, rct /*new Rectangle(rct.X, rct.Y, rct.Width, rct.Height)*/);

                if (RackMetre.ContainsKey(p.Key))
                {
                    rct = GetLocation(p.Key, Convert.ToInt32(RackMetre[p.Key]), 1); //0:maxadet, 1:maxmt.
                    g.FillRectangle(semiRed2, rct);
                    if (RackMetre[p.Key] > 0)
                        g.DrawString(
                            (p.Key < 1000 ? p.Key.ToString() :
                            p.Key<1100? "P" + p.Key.ToString().Substring(2):
                            p.Key<1200? "Y" + p.Key.ToString().Substring(2):
                            p.Key<1300? "T" + p.Key.ToString().Substring(2):
                            string.Empty),
                            //RackMetre[p.Key].ToString(),
                            new Font("Calibri", 9),
                            new SolidBrush(Color.DarkRed),
                            rct.X + 4, rct.Y + rct.Height / 2 - 4);
                }
                //g.DrawLine(penRed1, new Point(rct.X + rct.Width, rct.Y), new Point(rct.X + rct.Width, rct.Y + rct.Height));
            }

        }

        Rectangle GetLocation(int x, params int[] p)
        {
            int Aisle = x / 100;
            int Rack = x % 100;
            Rectangle r = new Rectangle();
            if (Aisle < 10)
            {
                r.X = (9 - Aisle) / 2 * (AisleW + 2 * RackW) + ((Aisle - 1) % 2) * RackW;
                r.Y = (11 - Rack) * RackH + (Rack < 6 ? AisleW : 0);
            }
            else //paletler
            {
                r.X = Aisle / 2 * (AisleW + 2 * RackW) + ((Aisle - 1) % 2) * RackW;
                r.Y = Rack * RackH; // +(Rack < 6 ? AisleW : 0);
            }

            r.Width = RackW;
            r.Height = RackH;

            if (p.Length > 0)
            {
                r.Width = (int)(r.Width * (double)p[0] / ((int)p[1] == 0 ? MaxAdet : MaxMetre));
                //r.Y += (int)p[1] * RackH / 2;
                //r.Height /= 2;
            }

            r.X = (int)(r.X * ScaleH) + Offset;
            r.Y = (int)(r.Y * ScaleV) + Offset;
            r.Width = (int)(r.Width * ScaleH);
            r.Height = (int)(r.Height * ScaleV);

            return r;
        }
        #endregion


    }
}